import store from "../store/index"
import {apiServe} from './api'

function formatNumber (n) {
  const str = n.toString()
  return str[1] ? str : `0${str}`
}

function formatTime (time, showTime=true) {
  const data = new Date(time)
  const year = data.getFullYear()
  const month = data.getMonth() + 1
  const day = data.getDate()

  const hour = data.getHours()
  const minute = data.getMinutes()
  const second = data.getSeconds()

  const t1 = [year, month, day].map(formatNumber).join('-')
  const t2 = [hour, minute, second].map(formatNumber).join(':')
  if(showTime) {
    return `${t1} ${t2}`
  }
  return t1
}

/** 数字千分位格式化,保留至少两位小数(reserveFloat是否保留浮点数，默认保留) */
/*
*num(传入的数值);defaultV(等于0时的显示问题，true:"0.00",false:"");
*floatLength(保留小数点长度);reserveFloat(是否保留小数点)
*/
function thousandF (num, defaultV = true,floatLength=2, reserveFloat = true) {
  if (num === "" ) {
    // 避免.00的状况出现
    return defaultValue;
  }
  if (num == 0 || num) {
    let arr = num.toString().split(".");
    let iValue = arr[0].replace(/\d{1,3}(?=(\d{3})+(\.|$))/g, "$&,");
    if (reserveFloat) {
      let fValue ;
      // 整数的话补小数位00，只有一位小数后面补一个0，两位以上小数直接使用原小数值
      if(floatLength==2){
        fValue =
        "." +
        (arr.length == 2 ? (arr[1].length >= 2 ? arr[1].slice(0,2) : arr[1] + "0") : "00");
      }else if(floatLength==4) {
        fValue = "."+(arr[1]?arr[1].slice(0,4):"0000");
      }
      return (iValue + fValue);
    } else {
      return iValue;
    }
  } else {
    if(defaultV){
      return defaultValue;
    }else{
      return ""
    }
  }
};
function delNull(str) {
  let newStr = str;
  if(typeof(str) == 'string') {
    newStr = str.replace(/\s|,/g, '');
  }
  return parseFloat(newStr);
}

function  RouterLinkFn(url,isHref = true ,cb) {
  if(!store.getters.isLogin) {
    wx.showModal({
      title: "提示",
        content: '系统检测暂未登录',
        confirmText: "立即登录",
        success: (res) => {
          if(res.confirm) {
            wx.navigateTo({
              url: "/pages/login/main",
              success: res => {
                this.$store.dispatch('clearStates');
              }
            });
          }
        }
    })
    return;
  }
  if(isHref) {
    wx.navigateTo({
      url: url,
      success: res => {
        if(cb) cb();
      }
    })
  } else {
    if(cb) cb();
  }
}

/**
 * 跳转第三方小程序
 */
function linkToMiniProgram(shortLink = '#小程序://智慧小区物业版/3wT3bLiICBCPo5C') {
  wx.navigateToMiniProgram({
    shortLink: shortLink,
    success(res) {
      // 打开成功
    }
  })
} 

/**
 * 调用拨打电话功能
 */
function makePhoneCall(phoneNumber = '12345678910') {
  wx.makePhoneCall({
    phoneNumber: phoneNumber
  })
} 

/**
 * 输入框数据绑定
 */
function inputValue() {
  if(arguments.length >= 4) {
    arguments[1][arguments[2]] = arguments[0].detail;
    if(arguments[arguments.length-1]) {
      arguments[arguments.length-1](arguments[1][arguments[2]])
    }
  } else if(arguments.length < 4) {
    arguments[1][arguments[2]] = arguments[0].detail;
  } else if(arguments.length <3) {
    this[arguments[1]] = arguments[0].detail;
  }
} 

/**附件上传 */
function fileUpload(data, cb) {
  wx.uploadFile({
    url: `${apiServe}file/mp/upload`,
    filePath: data,
    name: 'image',
    success: res => {
      let result = JSON.parse(res.data);
      if(result.code == 200) {
        cb(result.data);
      }
    }
  })
}
export default {
  formatNumber,
  formatTime,
  thousandF,
  delNull,
  RouterLinkFn,
  inputValue,
  fileUpload
}
